﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace BoAddonBiz_FreightForwarder.AddonPrint
{
    class B1Form_SelectPrintTemplate : SAPBoAddon.B1Form
    {
        public B1Form_SelectPrintTemplate()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_SelectPrintTemplate_frm_ItemEvent);
        }

        #region 继承基类
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            this.FormSetting.FormType = B1FormTypes.ado_AVA_PT_SelectedPrint;
            this.FormSetting.FormFileName = "AVA_SelectedPrintForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        #endregion
        void B1Form_SelectPrintTemplate_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction == false)
            {
                if (pVal.ItemUID == "Grid" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_CLICK && pVal.Row > -1)
                {
                    SelectedRow(pVal.Row);
                }
                if (pVal.ItemUID == "Grid" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK && pVal.Row > -1)
                {
                    SelectedRow(pVal.Row);
                    this.SelectGridRow();
                }
            }
            else
            {
                if (pVal.ItemUID == "Preview" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    SelectGridRow();
                }
                if (pVal.ItemUID == "Print" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    Printing();
                }
            }
        }
        private void SelectedRow(int row)
        {
            SAPbouiCOM.Grid Grid = this.CurrentForm.Items.Item("Grid").Specific;
            bool IsServer = Grid.DataTable.GetValue("U_IsServer", row).ToString().Trim() == "Y" ? true : false;
            if (IsServer)
                SAPBoAddon.B1Assistant.Item.SetVisible(CurrentForm, "Print", true);
            else
                SAPBoAddon.B1Assistant.Item.SetVisible(CurrentForm, "Print", false);
            Grid.Rows.SelectedRows.Add(row);
        }
        private void SelectGridRow()
        {
            SAPbouiCOM.Grid Grid = this.CurrentForm.Items.Item("Grid").Specific;
            int SelectedRow = Grid.Rows.SelectedRows.Item(0, SAPbouiCOM.BoOrderType.ot_RowOrder);
            string FileName = Grid.DataTable.GetValue("U_FileName", SelectedRow).ToString().Trim();
            string DocEntry = CurrentForm.DataSources.UserDataSources.Item("DocEntry").ValueEx.ToString();
            bool IsServer = Grid.DataTable.GetValue("U_IsServer", SelectedRow).ToString().Trim() == "Y" ? true : false;
            Utils.SetPrintSetting(SBOApp, FileName, DocEntry);
        }
        private void Printing()
        {
            #region 判断是否在服务器上打印
            SAPbouiCOM.Grid Grid = this.CurrentForm.Items.Item("Grid").Specific;
            int SelectedRow = Grid.Rows.SelectedRows.Item(0, SAPbouiCOM.BoOrderType.ot_RowOrder);
            string FileName = Grid.DataTable.GetValue("U_FileName", SelectedRow).ToString().Trim();
            string DocEntry = CurrentForm.DataSources.UserDataSources.Item("DocEntry").ValueEx.ToString();
            bool IsServer = Grid.DataTable.GetValue("U_IsServer", SelectedRow).ToString().Trim() == "Y" ? true : false;
            if (IsServer)
            {
                bool ReportFlag = false;
                PrintService.PrintServiceClient Client = new PrintService.PrintServiceClient();
                string Printer = System.Configuration.ConfigurationManager.AppSettings["Printer"];
                SAPbobsCOM.Recordset rs = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                rs.DoQuery(@"SELECT T2.U_Printer
                                FROM OUSR T0
	                                INNER JOIN OHEM T1 ON T0.USERID = T1.userId
	                                INNER JOIN [@AVA_OHEM] T2 ON T1.U_BusinessType = T2.Code
                                Where T0.USERID = " + SBOCompany.UserSignature.ToString());
                if (rs.RecordCount > 0)
                    if (string.IsNullOrEmpty(rs.Fields.Item("U_Printer").Value.ToString().Trim()) == false)
                    {
                        Printer = rs.Fields.Item("U_Printer").Value.ToString().Trim();
                    }
                if (string.IsNullOrEmpty(Printer))
                    ReportFlag = Client.PrintCommandDefaultPrinter(FileName, DocEntry, SBOCompany.UserName);
                else
                    ReportFlag = Client.PrintCommand(FileName, DocEntry, Printer, SBOCompany.UserName);
                Client.Close();
                if (ReportFlag)
                    SBOApp.MessageBox("打印成功！");
                else
                    SBOApp.MessageBox("打印机问题导致打印失败！");
            }
            #endregion

        }
    }
}
